package com.jinghua.treatment.controller;

import com.jinghua.treatment.common.GlobalConstant;
import com.jinghua.treatment.common.controller.BaseController;
import com.jinghua.treatment.common.domain.JSONResult;
import com.jinghua.treatment.entity.SysAuthentication;
import com.jinghua.treatment.entity.SysDoctor;
import com.jinghua.treatment.service.SysAuthenticationService;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

/**
 * 医生认证控制器
 * Created by 王兴光 on 2018/5/17.
 */
@Controller
@RequestMapping("/authentication")
public class SysAuthenticationController extends BaseController {
    private Logger log = Logger.getLogger(this.getClass());
    @Resource
    private SysAuthenticationService sysAuthenticationService;

    @ApiOperation(value = "更新认证状态")
    @RequestMapping(value = "/modifyAuthentication",method = RequestMethod.POST)
    public String insertAuthentication(SysAuthentication authentication , @RequestParam("fileName") MultipartFile attach){
        String newFileName = "";    // 存储图片唯一名称
        // 验证是否有文件需要上传
        if(!attach.isEmpty()){
            log.info("医生认证：有文件需要上传:"+attach.getOriginalFilename());
            // 定义上传路径
            String savePath = super.request.getServletContext().getRealPath("static"+ File.separator+"upload");
        }

        // 获取当前医生信息
        SysDoctor currentUser = (SysDoctor)super.session.getAttribute("currentDoctor");
        authentication.setDoctorId(currentUser.getDoctorId());  // 为认证信息对象添加医生编号
        // 查看是否有认证信息记录
        if(sysAuthenticationService.selectByPrimaryKey(currentUser.getDoctorId())==null){
            // 插入认证信息
            sysAuthenticationService.insert(authentication);
        }else{
            // 更新认证信息
            sysAuthenticationService.updateByPrimaryKey(authentication);
        }
        return "";
    }

    @ApiOperation(value = "获取当前医生的认证信息")
    @RequestMapping(value = "/getAuthentication",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult getAuthentcation(){
       // 获取当前医生信息
       SysDoctor currentUser = (SysDoctor)super.session.getAttribute("currentDoctor");
       return new JSONResult(GlobalConstant.SUCCESS_CODE,"获取认证信息",sysAuthenticationService.selectByPrimaryKey(currentUser.getDoctorId()));
   }

    @ApiOperation(value = "获取认证信息列表")
    @RequestMapping(value = "/getAuthenticationList",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult getAuthentcationList(){
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"获取认证信息列表",sysAuthenticationService.getAuthenticationList(super.pager));
    }
}
